<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getDictionarys">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

    <!--列表-->
    <el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="唯一标识" width="100">
      </el-table-column>
      <el-table-column prop="title" label="名称" width="100">
      </el-table-column>
      <el-table-column prop="intro" label="简介" width="100">
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="80">
        <template scope="scope">
          <span v-if="scope.row.status == 0" style="color: red;">禁用</span>
          <span v-if="scope.row.status == 1">启用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="450">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="success" size="small" @click="handleSearch(scope.$index, scope.row)">查询字典</el-button>
          <el-button type="warning" size="small" @click="handleDetail(scope.$index, scope.row)">新增明细</el-button>

        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="block">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="query.currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.total"
          style="float:right;">
      </el-pagination>
    </el-col>

<!--    <el-dialog title="新增明细弹框" :visible.sync="addDetailVisible" :close-on-click-modal="false">-->
<!--      <el-form :model="detailForm" label-width="80px" ref="detailForm">-->
<!--        <el-form-item label="sn" prop="唯一标识">-->
<!--          <el-input v-model="detailForm.sn" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="标题" prop="title">-->
<!--          <el-input v-model="detailForm.title" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="描述" prop="icon">-->
<!--          <el-input v-model="detailForm.intro" auto-complete="off"></el-input>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="状态">-->
<!--          <el-radio-group v-model="detailForm.status">-->
<!--            <el-radio class="radio" :label="1">启用</el-radio>-->
<!--            <el-radio class="radio" :label="0">禁用</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
<!--      </el-form>-->
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button @click.native="addDetailVisible = false">取消</el-button>-->
<!--        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->

    <el-dialog title="编辑页面" :visible.sync="addDetailVisible" :close-on-click-modal="false">
      <el-form :model="detailForm" label-width="80px" ref="saveForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="detailForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="值" prop="value">
          <el-input v-model="detailForm.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sequence">
          <el-input v-model="detailForm.sequence" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="icon">
          <el-input v-model="detailForm.intro" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="数据字典" prop="dictionary">
          <el-input v-model="detailForm.dictionary.title"  auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addDetailVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit1" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑页面" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="sn" prop="唯一标识">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="saveForm.title" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="icon">
          <el-input v-model="saveForm.intro" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.status">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				filters: {
					name: ''
				},
        pageInfo: {
				  rows:[],
          total:0
        },
        query: {
          currentPage: 1,
          pageSize: 10,
          keyword:""
        },
        dictionaryTree: [],
        dictionarys: [],
				total: 0,
				page: 1,
				listLoading: false,
				sels: [],//列表选中列


				saveLoading: false,

				//编辑界面数据

        addDetailVisible:false,
				saveFormVisible: false,//新增界面是否显示
				saveFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//新增界面数据
				saveForm: {
					sn: '',
					title: '',
          intro: '',
          status:1
				},
        detailForm:{
          title: '',
          value:'',
          sequence:0,
          intro: '',
          dictionary:{
            id:null,
            title:''
          }
        }

			}
		},
		methods: {
      getDictionarys1(){
        this.$http.get("/dictionary").then(res=>{
          if(res.data.success){
            this.dictionarys=res.data.obj;
          }
        })
      },
      saveSubmit1: function () {
        this.$refs.saveForm.validate((valid) => {
          console.log("de")
          console.log(this.detailForm)
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.$http.put("/dictionaryitem",this.detailForm).then((res) => {
                if(res.data.success) {
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  });
                  this.addDetailVisible = false;
                  // this.query.currentPage=1;
                  this.getDictionaryitems();
                }
              });
            });
          }
        });
      },
      handleDetail(index, row){
        this.addDetailVisible=true;
        this.detailForm.dictionary.title=row.title
        this.detailForm.dictionary.id=row.id

      },
      handleSearch(index, row){
        console.log("row")
        console.log(row)
        this.$router.push({ path: '/dictionaryitem/'+row.id });
      },
			handleCurrentChange(val) {
				this.query.currentPage = val;
				this.getDictionarys();
			},
      handleSizeChange(val) {
        this.query.pageSize=val;
        this.getDictionarys();
      },
			getDictionarys() {
				this.listLoading = true;

				this.$http.post("/dictionary",this.query).then(result=>{
          this.listLoading = false;
          if(result.data.success){
            this.pageInfo.rows=result.data.obj.pageList;

            this.pageInfo.total=result.data.obj.total;
          }else {
            this.$message({
              message: '查询失败',
              type: 'info'
            });
          }
        })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
          console.log(row.id);
          this.$http.delete("/dictionary/"+row.id).then((res) => {
						this.listLoading = false;
						if(res.data.success){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.query.currentPage=1;
              this.getDictionarys();
            }else {
              this.$message({
                message: '删除失败',
                type: 'info'
              });
            }
						//NProgress.done()
					});
				}).catch(() => {
          this.$message({
            message: '网络出错了',
            type: 'info'
          });
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				this.saveFormVisible = true;
        this.getDictionarys1();
        this.getTree();
        this.saveForm = row

			},
      getTree(){
			  this.$http.get("/dictionary/tree").then(res=>{

          if(res.data.success){
			      this.dictionaryTree=res.data.obj;

          }
        })
      },
			//显示新增界面
			handleAdd: function () {
        console.log("xinzen")
				this.saveFormVisible = true;
        this.getTree();
        this.getDictionarys1();
				this.saveForm = {
          sn: '',
          title: '',
          intro: '',
          status:1
        }
			},

			//新增
      saveSubmit: function () {
				this.$refs.saveForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.$http.put("/dictionary",this.saveForm).then((res) => {
                if(res.data.success) {
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  });
                  this.saveFormVisible = false;
                  this.query.currentPage=1;
                  this.getDictionarys();
                }
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
          this.listLoading = true;
          console.log(ids);
          this.$http.patch("/dictionary",ids).then((res) => {
            this.listLoading = false;
            if(res.data.success){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              this.query.currentPage=1;
              this.getDictionarys();
            }else {
              this.listLoading = false;
              this.$message({
                message: '删除失败',
                type: 'info'
              });
            }
            //NProgress.done()
          });
				}).catch(() => {
          this.listLoading = false;
				});
			}
		},
		mounted() {
			this.getDictionarys();
		}
	}

</script>

<style scoped>

</style>